package com.huluobo.a2208kotlindouyindemo.shop

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import com.huluobo.a2208kotlindouyindemo.R
import com.huluobo.a2208kotlindouyindemo.adapter.ShopAdapter
import com.huluobo.a2208kotlindouyindemo.cart.CartActivity
import com.huluobo.a2208kotlindouyindemo.databinding.ActivityShopBinding
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class ShopActivity : BaseActivity<ActivityShopBinding, ShopViewModel>() {
    private lateinit var shopAdapter: ShopAdapter
    override fun initData() {
        vm.getGoodsList(0, 1, 10)
        vm.getGoodsSuccess.observe(this) {
            shopAdapter.data.clear()
            shopAdapter.data.addAll(it)
            shopAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        binding.shopRv.layoutManager = GridLayoutManager(this, 2)
        shopAdapter = ShopAdapter()
        binding.shopRv.adapter = shopAdapter

        shopAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, DetailActivity::class.java)
            intent.putExtra(Const.PARAM_GOODS_ID, shopAdapter.data[position].id)
            startActivity(intent)
        }

        binding.shopToCartBtn.setOnClickListener {
            val intent = Intent(this, CartActivity::class.java)
            startActivity(intent)
        }
    }
}